/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        if(head==nullptr){return true;}
        ListNode* slow=head, *fast=head;
        while(fast && fast->next){
            slow=slow->next;
            fast=fast->next->next;
        }
        ListNode* cur=slow,*nextnode=slow->next;
        while(nextnode){
            ListNode* tmp=nextnode->next;
            nextnode->next=cur;
            cur=nextnode;
            nextnode=tmp;
        }
        slow->next=nullptr;
        while(head&&cur){
            if(head->val!=cur->val){return false;}
            head=head->next;
            cur=cur->next;
        }
        return true;
    }
};

